package name.fowler.refactoring.simplestore.calculator;

import java.util.Collection;

import name.fowler.refactoring.simplestore.domain.Rental;

public class SimpleRentalAmountCalculator implements RentalAmountCalculator {

	/* (non-Javadoc)
	 * @see name.fowler.refactoring.simplestore.calculator.RentalAmountCalculator#calculateRentalAmount(name.fowler.refactoring.simplestore.Rental)
	 */
	public double calculateRentalAmount(final Rental rental) {
		switch(rental.getMovie().getPriceCode()) {
			case Regular:
				return calculateRegularMovieAmount(rental);
			case NewRelease:
				return calculateNewReleaseMovie(rental);
			case Childrens:
				return calculateChildrenMovie(rental);
			default:
				return 0;
		}
	}
	
	public double calculateRentalAmountForAll(Collection<Rental> rentals) {
		double totalAmount = 0;
		for (Rental rental: rentals) {
			totalAmount += calculateRentalAmount(rental);
		}
		return totalAmount;
	}
	
	private double calculateChildrenMovie(final Rental rental) {
		return rental.getDaysRented() > 3 ? 
				((rental.getDaysRented() - 3) * 1.5) : 1.5;
	}

	private double calculateNewReleaseMovie(final Rental rental) {
		return rental.getDaysRented() * 3;
	}

	private double calculateRegularMovieAmount(Rental rental) {
		return rental.getDaysRented() > 2 ?
				((rental.getDaysRented() - 2) * 1.5 + 2) : 2;
	}
	
}
